package com.aruba.itemtouchhelperapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aruba.itemtouchhelperapplication.itemtouchhelper.ItemTouchHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IMoveAndSwipeCallback {
    RecyclerView rv;
    private List<String> strings;
    ItemTouchHelper itemTouchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rv.addItemDecoration(new MyDecoration(this, MyDecoration.HORIZONTAL_LIST));

        initData();
        MyAdapter myAdapter = new MyAdapter(this);
        rv.setAdapter(myAdapter);

        //给RecyclerView设置ItemTouchHelper
        ItemTouchHelperCallback itemTouchHelperCallback = new ItemTouchHelperCallback();
        itemTouchHelperCallback.setiMoveAndSwipeCallback(this);
        itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
        itemTouchHelper.attachToRecyclerView(rv);
    }

    private void initData() {
        strings = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strings.add(String.valueOf(i));
        }
    }

    @Override
    public void onMove(int prePosition, int postPosition) {
        Collections.swap(strings, prePosition, postPosition);
        if (rv.getAdapter() != null) {
            rv.getAdapter().notifyItemMoved(prePosition, postPosition);
        }
    }

    @Override
    public void onSwiped(int position) {
    }

    @Override
    public void onChildDraw(RecyclerView.ViewHolder viewHolder, float dX, float dY) {
//        if (isClick) {
//            return;
//        }
        MyAdapter.MyViewHolder myViewHolder = (MyAdapter.MyViewHolder) viewHolder;
        //最大偏移不超过删除布局宽度
        if (dX < -myViewHolder.ll_remove.getWidth()) {
            dX = -myViewHolder.ll_remove.getWidth();
        }
        myViewHolder.tv.setTranslationX(dX);
    }

    @Override
    public View getItemFrontView(RecyclerView.ViewHolder mPreOpened) {
        return mPreOpened.itemView.findViewById(R.id.tv);
    }

    private boolean isClick = false;

    class MyAdapter extends RecyclerView.Adapter {
        private Context context;

        public MyAdapter(Context context) {
            this.context = context;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_rv, viewGroup, false));
            myViewHolder.tvRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) v.getTag();
                    strings.remove(viewHolder.getAdapterPosition());
                    notifyItemRemoved(viewHolder.getAdapterPosition());
                }
            });
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
            myViewHolder.tv.setText(strings.get(i));

            myViewHolder.tvRemove.setTag(viewHolder);
        }

        @Override
        public int getItemCount() {
            return strings.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView tv;
            public LinearLayout ll_remove;
            public TextView tvRemove;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv);
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        itemTouchHelper.close();
                    }
                });
                ll_remove = itemView.findViewById(R.id.ll_remove);
                tvRemove = itemView.findViewById(R.id.tv_remove);
                itemView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isClick = true;
                        itemTouchHelper.close();
                    }
                });
            }
        }
    }
}
